<template>
  <div>
    <el-form inline :model="queryData" ref="queryData" class="query-form">
      <el-form-item label="商品订单ID:" prop="id">
        <el-input type="number" v-model="queryData.id" placeholder="请输入" style="width: 200px" clearable
                  @clear="formClear('id')"/>
      </el-form-item>

      <el-form-item label="服务商品名称:" prop="goodsName">
        <el-input v-model="queryData.goodsName" placeholder="请输入" style="width: 200px" clearable
                  @clear="formClear('goodsName')"/>
      </el-form-item>

      <el-form-item label="联系方式:" prop="purchasePhone">
        <el-input v-model="queryData.purchasePhone" placeholder="请输入" style="width: 200px" clearable
                  @clear="formClear('purchasePhone')"/>
      </el-form-item>
      <el-form-item label="支付状态:" prop="paymentState">
        <el-select v-model="queryData.paymentState" placeholder="请选择" clearable @clear="formClear('type')"
                   style="width: 200px">
          <el-option label="待支付" value="0"/>
          <el-option label="已支付" value="1"/>
          <el-option label="已超时" value="2"/>
          <el-option label="服务中" value="3"/>
          <el-option label="已结束" value="4"/>
        </el-select>
      </el-form-item>

      <el-form-item label="生成时间:" prop="createTime">
        <el-date-picker v-model="queryData.createTime" value-format="yyyy-MM-dd" type="date" style="width: 200px"
                        @clear="formClear('createTime')" placeholder="选择日期"></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">查 询</el-button>
        <el-button @click="onReset">重 置</el-button>
        <el-button type="primary" @click="$router.push('/serviceCommodityManage/serviceCommodityAdd')">新 增</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableList" border style="width: 100%;">
      <el-table-column prop="id" label="商品订单ID" align="center" width="90"/>
      <el-table-column prop="goodsName" label="服务商品名称" align="center"/>
      <el-table-column prop="purchasePhone" label="服务方联系方式" align="center"/>
      <el-table-column prop="orderMoney" label="订单金额" align="center"/>
      <el-table-column prop="purchaseAmount" label="购买数量" align="center"/>

      <el-table-column prop="paymentState" label="服务状态" align="center" :formatter="(row)=>formatter(row, 1)"/>
      <el-table-column prop="orderNo" label="支付订单ID" align="center" width="200" show-overflow-tooltip/>
      <el-table-column prop="paymentState" label="支付状态" align="center" :formatter="(row)=>formatter(row, 2)"/>
      <el-table-column prop="createTime" label="生成时间" align="center" width="150"/>
      <el-table-column prop="paymentTime" label="支付时间" align="center" width="150"/>
      <el-table-column prop="endTime" label="结束时间" align="center" width="150"/>
      <el-table-column label="操作" align="center" width="150" fixed="right">
        <template slot-scope="{row}">
          <el-button type="text" @click="entityInfoEvent(row)" v-if="row.paymentState == 1 || row.paymentState == 3">
            {{row.paymentState == 1 ? '开始服务' : row.paymentState == 3 ? '结束服务' : ''}}
          </el-button>
          <span v-else-if="row.paymentState == 4">结束服务</span>
        </template>
      </el-table-column>
    </el-table>
    <Paging v-model="queryData" @change="getRequest"></Paging>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'

  export default {
    computed: {
      ...mapGetters(['confirm'])
    },
    name: 'commodityOrderManage',
    data() {
      return {
        typeList: [],
        tableList: [],
        queryData: {
          id: null,
          goodsName: null, purchasePhone: null, paymentState: null, createTime: null,
          pageNum: 1,
          pageSize: 10,
          total: 0
        }
      }
    },
    beforeMount() {
      this.getRequest()
    },
    methods: {
      // 状态 0 待支付 1 已支付 2 已超时 3 服务中 4 已结束
      formatter(row, status) {
        if (status == 2) {
          switch (row.paymentState) {
            case 0 :
              return  '待支付'
            case 1 :
              return '已支付'
            case 2 :
              return '已超时'
            case 3 :
              return '已支付'
            case 4 :
              return '已支付'
          }
        } else {
          switch (row.paymentState) {
            case 0 :
              return '待确认支付'
            case 1 :
              return '待确认服务'
            case 2 :
              return '订单超时'
            case 3 :
              return '服务中'
            case 4 :
              return '已结束'
          }
        }
      },
      /**
       * 查询条件提交事件
       */
      onSubmit() {
        this.queryData.pageNum = 1
        this.getRequest()
      },
      /**
       * 重置事件
       */
      onReset() {
        this.$refs['queryData'].resetFields()
        this.getRequest()
      },
      /**
       * 列表请求事件
       */
      async getRequest() {
        const queryData = JSON.parse(JSON.stringify(this.queryData))
        const res = await this.$ajax.get('/wx/order/backstage/list', { params: queryData })
        this.queryData.total = res.total
        this.tableList = res.rows
      },
      entityInfoEvent(row) {
        if (row.paymentState != 1 && row.paymentState != 3) return
        const paymentState = row.paymentState == 1 ? 3 : 4
        this.$confirm('操作将无法修改是否继续? ', '提示', this.confirm.options).then(async() => {
          await this.$ajax.get(`/wx/order/endService?id=${row.id}&paymentState=${paymentState}`)
          this.$message.success('操作成功!')
          this.getRequest()
        })
      },
      /**
       * form置空事件
       */
      formClear(key) {
        this.queryData[key] = null
        this.onSubmit()
      }
    }
  }
</script>

<style scoped>
::v-deep input::-webkit-outer-spin-button,
::v-deep input::-webkit-inner-spin-button {
  -webkit-appearance: none !important;
}
/deep/ input[type='number'] {
  -moz-appearance: textfield !important;
  }
</style>
